package com.xumt.hwHMSdemo;

import com.xumt.hwHMSdemo.searchresult;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.huawei.hms.site.api.SearchResultListener;
import com.huawei.hms.site.api.SearchService;
import com.huawei.hms.site.api.SearchServiceFactory;
import com.huawei.hms.site.api.model.Coordinate;
import com.huawei.hms.site.api.model.DetailSearchRequest;
import com.huawei.hms.site.api.model.DetailSearchResponse;
import com.huawei.hms.site.api.model.HwLocationType;
import com.huawei.hms.site.api.model.LocationType;
import com.huawei.hms.site.api.model.NearbySearchRequest;
import com.huawei.hms.site.api.model.NearbySearchResponse;
import com.huawei.hms.site.api.model.SearchStatus;
import com.huawei.hms.site.api.model.Site;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import com.xumt.hwHMSdemo.Locationkit;
public class NearbySearchActivity extends AppCompatActivity {
    private  static searchresult[] myresults;
    private int resultnumber=0;
    private SearchService searchService;
    private String key;
    private List<Site> sites;
    public static final String Longitude="com.demo.myfirstdemo.longitude";
    public static final String Lititude="com.demo.myfirstdemo.lititude";
//    public String thisjingdu;
//    public String thisweidu;
    private String name[]=new String[5];
    private Double jingdu;
    private Double weidu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby_search);
        AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NearbySearchActivity.this, Detailed.class);
                for (Site site : sites) {
                    if (site.getName() == name[position]) {
                        intent.putExtra(Detailed.storemessage, site.getSiteId());
                        intent.putExtra(Detailed.myLongitude,jingdu );
                        intent.putExtra(Detailed.myLititude, weidu);
                        startActivity(intent);
                    }
                }
            }
        };
        Intent intent = getIntent();
     jingdu = intent.getExtras().getDouble(Longitude);
weidu = intent.getExtras().getDouble(Lititude);
//        thisjingdu=intent.getExtras().getString(Longitude);
//        thisweidu=intent.getExtras().getString(Lititude);
        TextView thisjingdu=findViewById(R.id.thisjingdu);
        thisjingdu.setText(jingdu.toString());
        TextView thisweidu=findViewById(R.id.thisweidu);
        thisweidu.setText(weidu.toString());
        // 声明SearchService对象
        try {
            key=URLEncoder.encode("CgB6e3x9W985/nvaN5rd75cS5n7LEHs5wnWoEpG2sU21mBQZu4sryiiENsvarFzfa5zHvydkQpFF6cczRzbSNai5", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
// 创建SearchService实例
        searchService = SearchServiceFactory.create(this, key);
        // 创建请求体
        NearbySearchRequest request = new NearbySearchRequest();
        //  Coordinate location = new Coordinate(118.092985, 24.486986);
        // Coordinate location = new Coordinate(48.893478, 2.334595);
        Coordinate location = new Coordinate(weidu,jingdu);
        request.setLocation(location);
        request.setQuery("宠物商店");
        request.setRadius(50000);
        //    request.setHwPoiType(HwLocationType.BUSINESS);
        request.setLanguage("zn");
        request.setPageIndex(1);
        request.setPageSize(5);
//
//        request.setStrictBounds(false);
//        NearbySearchRequest request = new NearbySearchRequest();
//        Coordinate location = new Coordinate(jingdu,weidu);
//        request.setLocation(location);
//        request.setQuery("厦门");
//        request.setRadius(40000);
//        request.setHwPoiType(HwLocationType.ADDRESS);
//        request.setLanguage("zn");
//        request.setPageIndex(1);
//        request.setPageSize(5);
//        request.setStrictBounds(false);
        // 创建搜索结果侦听器
        SearchResultListener<NearbySearchResponse> resultListener = new SearchResultListener<NearbySearchResponse>() {
            // 正常结果返回
            @Override
            public void onSearchResult(NearbySearchResponse results) {
                if (results == null || results.getTotalCount() <= 0) {
                    return;
                }
                // List<Site> sites = results.getSites();
                sites = results.getSites();
                if(sites == null || ((List) sites).size() == 0){
                    return;
                }
                myresults=new searchresult[sites.size()];
                for (Site site : sites) {
                    searchresult searchresult=new searchresult();
                    searchresult.setid(site.getSiteId());
                    searchresult.setname(site.getName());
                    myresults[resultnumber]=searchresult;
                    name[resultnumber]=site.getName();
                    Log.i("TAG", String.format("siteId: '%s', name: %s\r\n", myresults[resultnumber].getid(), myresults[resultnumber].getname()));
                    resultnumber++;
//                    Log.i("TAG", String.format("siteId: '%s', name: %s\r\n", site.getSiteId(), site.getName()));
                }
                resultnumber=0;
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        NearbySearchActivity.this,android.R.layout.simple_list_item_1,name);
                ListView listView = findViewById(R.id.nearbyseaechoption);
                listView.setOnItemClickListener(itemClickListener);
                listView.setAdapter(adapter);//把构造好的适配器对象传进去
            }
            // 异常结果返回
            @Override
            public void onSearchError(SearchStatus status) {
                Log.i("TAG", "Error : " + status.getErrorCode() + " " + status.getErrorMessage());
            }
        };
// 调用周边搜索接口
        searchService.nearbySearch(request, resultListener);




//        DetailSearchRequest request1 = new DetailSearchRequest();
//        request1.setSiteId("554822821093066880");
//        request1.setLanguage("zn");
//        request1.setChildren(false);
//// 创建搜索结果侦听器
//        SearchResultListener<DetailSearchResponse> resultListener1 = new SearchResultListener<DetailSearchResponse>() {
//            // 正常结果返回
//            @Override
//            public void onSearchResult(DetailSearchResponse result) {
//                Site site;
//                if (result == null || (site = result.getSite()) == null) {
//                    return;
//                }
//                Log.i("TAG", String.format("siteId: '%s', name: %s,address:%s\r\n", site.getSiteId(), site.getName(),site.getFormatAddress()));
//            }
//            // 异常结果返回
//            @Override
//            public void onSearchError(SearchStatus status) {
//                Log.i("TAG", "Error : " + status.getErrorCode() + " " + status.getErrorMessage());
//            }
//        };
//// 调用地点详情接口
//        searchService.detailSearch(request1, resultListener1);
    }
    private void serchnearby(){

    }
}